<?php

namespace App\Http\Controllers\Site;

use App\B2BProject\Site\HelperControllerBuildTrait;
use Auth;
use App\B2BProject\Area;
use App\B2BProject\Custom\Business\Goods;
use App\B2BProject\Custom\Business\GoodsCategory;
use App\B2BProject\Custom\Business\GoodsImage;
use App\B2BProject\Custom\Business\Store;
use App\B2BProject\Custom\MemberInformation;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DistributionController extends Controller
{
    use HelperControllerBuildTrait;

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth', ['except' => ['getGoods', 'getChoose', 'getList']]);
    }

    /**
     * 显示供采分销区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function getList(Request $request)
    {
        if (\Request::ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));
            $query = Goods::display()->hasCover()->where('grant_status', 1)->where('strict_lock',false);
            if ($goodType = $request->query('goodType')) {
                $category = GoodsCategory::with('children')->whereId($goodType)->firstOrFail();
                if ($category->parent_id == 0) {
                    $in = [];
                    foreach ($category->children as $child) {
                        $in[] = $child->id;
                    }

                    $query = $query->whereIn('class_id', $in);
                } else {
                    $query = $query->where('class_id', $goodType);
                }
            }

            if ($request->query('time')) {
                $query = $query->orderBy('created_at', 'asc');
            } else {
                $query = $query->orderBy('created_at', 'desc');
            }
            if ($addr = $request->query('addr')) {
                $query = $query->where('province_id', $addr);
            }
            if ($name = $request->query('name')) {
                $query = $query->where('name', 'like', "%$name%");
            }
            $goodsInfo = $query->paginate($request->query('pageSize', 7));
            $data      = [];
            foreach ($goodsInfo as $info) {
                $item['goodsid'] = $info->id;
                $item['img']     = $info->goodsImage->first()->image;
                $item['name']    = $info->name;
                //登陆状态
                if (Auth::check()) {
                    if (Auth::user()->class == 2) {//采购商身份
                        $item['authority'] = $info->isGranted();
                        $item['price']     = $info->original_price;
                    } else {//供应商身份
                        $member = Auth::user();
                        if (Goods::where('store_id',
                            Store::where('member_id', $member->id)->first()->id)->first()
                        ) {   // 供应商有店铺
                            if (Goods::where('store_id',
                                    Store::where('member_id',
                                        $member->id)->first()->id)->first()->store_id == Goods::where('id',
                                    $info->id)->first()->store_id
                            ) { //供应商自己的店铺

                                $item['authority'] = true;
                                $item['price']     = $info->original_price;
                            } else {
                                $item['authority'] = false;
                                $item['price']     = '未授权';
                            }
                        } else {
                            $item['authority'] = false;
                            $item['price']     = '未授权';
                        }

                    }
                } else {
                    $item['authority'] = $info->isGranted();
                    $item['price']     = $info->original_price;
                }
                $item['minimum']  = $info->minimum_order;
                $item['supplier'] = $info->store->member->information->company;

                $item['store'] = $info->store->first()->id;
                $item['addr']  = Area::where('id', $info->province_id)->first()->name . Area::where('id',
                        $info->city_id)->first()->name;
                $item['href']  =
                    route('goods.detail', ['goods_id' => $info->id, 'page' => $request->query('page', $request->page)]);

                $supplier_id = $info->store->id;
                if (Auth::check()) {
                    $item['login_auth_href'] = "/store/$supplier_id/authorization-request";
                } else {
                    $item['login_auth_href'] = "passport/login";
                }
                $data[] = $item;

            }
            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $goodsInfo->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $goodsInfo->total(),
                ],
            ]);
        }
        return view('host/distribution');
    }
}
